package com.zhubayi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhubayi.entity.Great;
import com.zhubayi.entity.Video;
import com.zhubayi.entity.vo.CategoryVideoNumVo;
import com.zhubayi.entity.vo.PubTimeVo;
import com.zhubayi.entity.vo.VideoVo;
import com.zhubayi.mapper.GreatMapper;
import com.zhubayi.mapper.VideoMapper;
import com.zhubayi.service.CategoryService;
import com.zhubayi.service.VideoService;
import com.zhubayi.utils.VideoQueryConditionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 祝八一
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    GreatMapper greatMapper;
    @Override
    public List<Video> queryAll() {
        return  videoMapper.queryAll();

    }

    @Override
    public Video queryById(Integer id) {
        return videoMapper.queryById(id);
    }

    @Override
    public void deleteById(Integer id) {
        videoMapper.deleteById(id);
    }

    @Override
    public List<Video> queryByTitle(String title) {
        return videoMapper.queryByTitle(title);
    }

    @Override
    public List<VideoVo> getAll() {
        return videoMapper.getAll(null,null);
    }

    @Override
    public List<VideoVo> queryCondition(VideoQueryConditionUtils utils) {
        return  videoMapper.queryCondition(utils);

    }

    @Override
    public PageInfo<VideoVo> getForIndex(Integer categoryId,String title) {
        PageHelper.startPage(1, 15);  //开启分页，接收两个参数：页码及分页大小
        List<VideoVo> data = videoMapper.getAll(categoryId,title);

        PageInfo<VideoVo> pageInfo = new PageInfo<>(data); //包含分页的信息
        return pageInfo;
    }

    @Override
    public void update(Video video) {
        videoMapper.update(video);
    }

    @Override
    public void add(Video video) {
        videoMapper.addVideo(video);
    }

    @Override
    public void addViewNum(Integer id) {
        videoMapper.addViewNum(id);
    }



    @Override
    public List<Video> queryByUid(Integer id) {
       return videoMapper.queryByUserId(id);
    }

    @Override
    public List<VideoVo> queryVoByUid(Integer id) {
      return   videoMapper.queryVoByUid(id);

    }

    @Override
    public List<Video> queryFavor(Integer id) {
     return   videoMapper.queryFavor(id);
    }

    @Override
    public Map<String, Integer> getCategoryVideoNum() {
        Map<String, Integer> map=new HashMap<>();
        List<CategoryVideoNumVo> categoryVideoNum = new ArrayList();
        //查询所有
        categoryService.queryAll().stream().forEach(item->{
            CategoryVideoNumVo categoryVideoNumVo = new CategoryVideoNumVo();
            categoryVideoNumVo.setId(item.getId());
            categoryVideoNumVo.setCategoryId(item.getId());
            categoryVideoNumVo.setTitle(item.getName());
            categoryVideoNum.add(categoryVideoNumVo);
        });
        List<CategoryVideoNumVo> categoryVideoNumVos = videoMapper.queryCategoryVideoNum();
        for (CategoryVideoNumVo i : categoryVideoNum) {
            for (CategoryVideoNumVo j : categoryVideoNumVos) {
                if (i.getCategoryId().equals(j.getCategoryId())){
                    i.setNum(j.getNum());
                }
            }
            map.put(i.getTitle(),i.getNum()==null?0:i.getNum());
        }

        return map;
    }

    @Override
    public Map<String, Integer> getSortByViewNum() {
        List<Video> videos = videoMapper.queryOrderByViewNum(10);
        HashMap<String, Integer> map = new HashMap<>();
        for (Video video : videos) {
            map.put(video.getTitle(),video.getViewNum());
        }
        return map;
    }

    @Override
    public Map<String, Integer> getSortByPraiseNum() {
        List<Video> videos = videoMapper.queryOrderByPraiseNum(10);
        HashMap<String, Integer> map = new HashMap<>();
        for (Video video : videos) {
            map.put(video.getTitle(),video.getPraiseNum());
        }
        return map;
    }

    @Override
    public void addPraiseNum(Integer vid) {
        videoMapper.addPraiseNum(vid);
    }


    @Transactional

    @Override
    public Integer updateGreatNum(Integer vid, Integer uid) {
        //点赞表查询是否点赞
        Great great = greatMapper.queryByUidAndVid(uid, vid);
        if (great==null){
            //没点赞
            //点赞表表增加
            greatMapper.add(uid, vid);
            //视频表增加
            videoMapper.addGreatNum(vid);
            return videoMapper.queryById(vid).getGreatNum();
        }else {
            //取消点赞
            greatMapper.delete(uid, vid);
            //视频表增加
            videoMapper.readuceGreatNum(vid);
            return videoMapper.queryById(vid).getGreatNum();
        }
    }

    @Override
    public Map<String, Integer> queryPubTime() {
        List<PubTimeVo> pubTimeVos = videoMapper.queryPubTime();
        Map<String, Integer> map = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        pubTimeVos.stream().forEach(i->{

            map.put(dateFormat.format(i.getPub_time()),i.getNum());
        });
       return map;
    }

}
